<template>
  <div class="globalSearch">
    <a-input-search
      v-model="searchValue"
      placeholder="请输入"
      style="width: 200px"
      :allowClear="true"
      @search="onSearch"
    />
  </div>
</template>
<script>
export default {
  name: 'globalSearch',
  props: {
    /*父组件传过来的表格数据*/
    searchTableData: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      searchTable: this.searchTableData, // 被查询表格数据
      searchValue: "", // 查询条件
      handSearchTable: [], // 查询过的表格数据
    }
  },
  watch: {
    searchTableData(val, oldVal) {
      this.searchTable = val
    },
    searchValue(val, oldVal) {
      this.searchValue = val
      if (!this.searchValue) {
        this.onSearch()
      } 
      // else {
      //   this.$emit('onSearch', this.searchTableData, val)
      // }
    },
  },
  methods: {
    onSearch() {
      let _this = this
      _this.handSearchTable = []
      _this.searchTable.map(item => {
        let count = 0
        for (let k in item) {
          if (item[k] && k != 'oid' && k != '_token' && k != 'tenantid') {
            // let nowValue = item[k].toString()
            let searching = _this.searchValue.toLocaleLowerCase()
            let nowValue = item[k].toString().toLocaleLowerCase()
            if (nowValue.includes(searching)) {
              count++
            }
          }
        }
        if (count) {
          _this.handSearchTable.push(item)
        }
      })
      _this.$emit('onSearch', _this.handSearchTable, _this.searchValue)
    }
  }
}
</script>
<style lang="less" scoped>
</style>